package com.ihisun.tmhcms.core.service.projectservice.impl;

import com.ihisun.tmhcms.core.dao.projectdao.EditProjectClassDao;
import com.ihisun.tmhcms.core.dao.projectdao.ProjectClassDao;
import com.ihisun.tmhcms.core.dao.projectdao.ProjectDao;
import com.ihisun.tmhcms.core.dto.projectdto.EditProjectClassDto;
import com.ihisun.tmhcms.core.service.projectservice.EditprojectClassService;
import com.ihisun.tmhcms.core.vo.ProjectClassVo;
import com.ihisun.tmhcms.core.vo.ProjectVo;
import com.ihisun.tmhcms.utils.DateUtils;
import com.ihisun.tmhcms.utils.ResponseVo;
import com.ihisun.tmhcms.utils.UuIdUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EditprojectClassServiceImpl implements EditprojectClassService {
    @Resource
    EditProjectClassDao editProjectClassDao;
    @Resource
    ProjectClassDao projectClassDao;
    @Resource
    ProjectDao projectDao;

    @Override
    public ResponseVo editProjectClass(EditProjectClassDto editProjectClassDto) {
        if (editProjectClassDto.getProjectTypeId() != null && !"".equals(editProjectClassDto.getProjectTypeId())) {
            editProjectClassDto.setUpdateTime(DateUtils.getCurrentTime());
            EditProjectClassDto save = editProjectClassDao.save(editProjectClassDto);
        } else {
            editProjectClassDto.setCreateTime(DateUtils.getCurrentTime());
            editProjectClassDto.setUpdateTime(DateUtils.getCurrentTime());
            editProjectClassDto.setProjectTypeId(UuIdUtil.getToUuid());
            editProjectClassDto.setState(1);
            editProjectClassDao.save(editProjectClassDto);
        }
        return ResponseVo.ok();
    }


    @Override
    public ResponseVo editProjectClassState(String projectClassId, Integer state) {
        if (state.equals(3)) {
            List<ProjectVo> byProjectTypeIdAndStateNot = projectDao.findByProjectTypeIdAndStateNot(projectClassId, state);
            if (byProjectTypeIdAndStateNot != null && !byProjectTypeIdAndStateNot.isEmpty()) {
                return ResponseVo.error();
            }else{
                ProjectClassVo byProjectTypeId = projectClassDao.findByProjectTypeId(projectClassId);
                byProjectTypeId.setState(state);
                projectClassDao.save(byProjectTypeId);
                return ResponseVo.ok();
            }
        }else{
            ProjectClassVo byProjectTypeId = projectClassDao.findByProjectTypeId(projectClassId);
            byProjectTypeId.setState(state);
            projectClassDao.save(byProjectTypeId);
            return ResponseVo.ok();
        }

    }
}
